#!/bin/bash
# 参数
port=9092
ip=192.168.161.129
backPath=$(pwd)
homePath=/data/kafka
version=2.12-3.6.2
kafkaVersion=3.6.2
# 判断文件夹
echo "************************准备kafka安装空间*********************"
if [ -d "$homePath" ];then
    echo "$homePath 已存在，请移走"
    exit 0
else
    mkdir -p $homePath
    echo "$homePath 已创建"
fi
echo "************************准备kafka安装资源*********************"
if [ -f "$backPath/kafka_$version.tgz" ];then
    echo "安装包准备就绪"
else
    wget https://mirrors.aliyun.com/apache/kafka/$kafkaVersion/kafka_$version.tgz
fi
echo "************************开始安装kafka*************************"
tar -zxvf $backPath/kafka_$version.tgz -C $homePath
echo "************************准备kafka配置*************************"
# 抽取配置信息
cd $homePath/kafka_$version/config
cat zookeeper.properties | grep -v '^#' | grep -v '^$' > zk.properties
cat server.properties | grep -v '^#' | grep -v '^$' > kafka.properties
# 向kafka.properties追加配置，开启远程访问
echo "# 允许外部访问端口连接" >> kafka.properties
echo "listeners=PLAINTEXT://0.0.0.0:$port" >> kafka.properties
echo "#外部代理地址" >> kafka.properties
echo "advertised.listeners=PLAINTEXT://$ip:$port" >> kafka.properties
echo "************************启动kafka*****************************"
# 启动zookeeper
nohup $homePath/kafka_$version/bin/zookeeper-server-start.sh $homePath/kafka_$version/config/zk.properties >/dev/null 2>&1 &
# 启动kafka
nohup $homePath/kafka_$version/bin/kafka-server-start.sh $homePath/kafka_$version/config/kafka.properties >/dev/null 2>&1 &
# 验证启动
kafka_process=$(ps -ef|grep kafka|grep -v grep)
if [ "$kafka_process" != "" ]
then
    echo "***********************启动kafka成功**************************"
    echo "*            端口：$port"
    echo "*    外部代理地址：$ip"
    echo "**************************************************************"
else
    echo "kafka启动失败，请校验配置信息"
    exit 0
fi
